class MyStack:
    def __init__(self):
        self.mylist = []

    def add_stack(self, item):
        return self.mylist.append(item)

    def pop_stack(self):
        if not self.is_empty():
            return self.mylist.pop()

    def is_empty(self):
        return len(self.mylist) == 0

    def length(self):
        return len(self.mylist)


def stack_demo(expression):
    mystack = MyStack()
    dic = {')': "("}
    for char in expression:
        # 假设char是左括号
        if char in dic.values():
            mystack.add_stack(char)
        # 假设char是右括号
        elif char in dic.keys():
            if mystack.is_empty() or mystack.pop_stack() != dic[char]:
                return False
    return mystack.is_empty()


if __name__ == '__main__':
    expressions = ["(())", "((()))", "(()", ")(", "(a + b) * (c - d)"]
    for expr in expressions:
        print(f"表达式 '{expr}' 的括号匹配: {stack_demo(expr)}")



